package com.atguigu.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.entity.LendItem;
import com.atguigu.srb.core.service.LendItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-06-30
 */
@Api(tags = "会员投资模块")
@RestController
@RequestMapping("/api/core/lendItem")
public class ApiLendItemController {
    @Resource
    LendItemService lendItemService;
    //投资的form表单构建的接口
    @ApiOperation("投资form构建")
    @PostMapping("/invest/{lendId}/{investAmount}")
    public R invest(@RequestHeader("token")String token,
                    @PathVariable("lendId")Long lendId,
                    @PathVariable("investAmount")BigDecimal investAmount){
        String form = lendItemService.handleVest(token,lendId,investAmount);
        return R.ok().data("form",form);
    }

    //投资成功后hfb异步通知的回调接口
    //http://192.168.216.100/api/core/lendItem/notify
    @ApiOperation("投标成功后hfb异步通知的接口")
    @PostMapping("/notify")
    public String notify(@RequestParam Map<String,Object> map){
        /* String s = JSON.toJSONString(map);
        System.out.println("s = " + s);
        return "success"; */
        return lendItemService.handleCallback(map);
    }
    //会员的投资列表的详情里面
    @ApiOperation("投资记录的查询和展示")
    @GetMapping("/investRecord/{lendId}")
    public R getInvestRecord(@PathVariable("lendId")Long lendId){
        //按照时间降序排序,查询的状态是已支付1和已还款的2
        List<LendItem> list = lendItemService.list(Wrappers.lambdaQuery(LendItem.class).eq(LendItem::getLendId, lendId)
                .in(LendItem::getStatus,1,2).orderByDesc());
        return R.ok().data("items",list);
    }

}

